<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <el-icon class="logo-icon">
            <TrendCharts />
          </el-icon>
          <h1 class="title">样式测试系统</h1>
        </div>
        <p class="subtitle">请登录您的账户</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { TrendCharts, User, Lock } from '@element-plus/icons-vue'

export default {
  name: 'Login',
  components: {
    TrendCharts,
    User,
    Lock
  },
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '密码长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // 检查是否已经登录
    if (this.isLoggedIn()) {
      this.$router.replace('/')
    }
  },
  methods: {
    async handleLogin() {
      if (this.loading) return

      try {
        await this.$refs.loginFormRef.validate()
        
        this.loading = true
        
        // 模拟登录请求延迟
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        // 前端硬编码验证
        if (this.validateCredentials()) {
          // 登录成功，保存登录状态
          this.saveLoginState()
          
          this.$message.success('登录成功')
          
          // 获取重定向路径
          const redirect = this.$route.query.redirect || '/'
          this.$router.replace(redirect)
        } else {
          this.$message.error('用户名或密码错误')
        }
      } catch (error) {
        console.error('登录验证失败:', error)
      } finally {
        this.loading = false
      }
    },

    validateCredentials() {
      // 硬编码的用户名和密码
      const validCredentials = [
        { username: 'clj', password: 'a868733946' }
      ]

      return validCredentials.some(cred => 
        cred.username === this.loginForm.username && 
        cred.password === this.loginForm.password
      )
    },

    saveLoginState() {
      // 保存登录状态到本地存储
      const loginData = {
        username: this.loginForm.username,
        loginTime: new Date().getTime(),
        token: this.generateToken()
      }
      
      localStorage.setItem('user_login_data', JSON.stringify(loginData))
      localStorage.setItem('is_logged_in', 'true')
    },

    generateToken() {
      // 生成简单的token
      return btoa(this.loginForm.username + '_' + new Date().getTime())
    },

    isLoggedIn() {
      return localStorage.getItem('is_logged_in') === 'true'
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
}

.logo-icon {
  font-size: 32px;
  color: #667eea;
}

.title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.subtitle {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border: none;
}

.login-button:hover {
  background: linear-gradient(90deg, #5a67d8, #6b46c1);
}

.login-tips {
  margin-top: 20px;
}

.login-tips .el-alert {
  border-radius: 8px;
}

.login-tips p {
  margin: 5px 0;
  font-size: 13px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 20px;
  }
}

/* 动画效果 */
.login-card {
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>